package com.example.myapplication;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.IBinder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class BlankFragment2 extends Fragment {
    RecyclerView recyclerView;
    //    List list;
    Adapter adapter;

    Context context;

    MyService.Mybinder mybinder;
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.activity_adapter, container, false);  //绑定recyler的layout

        recyclerView= view.findViewById(R.id.recyler);

        List<Map<String,Object>> list=new ArrayList<>();

//        String[] name={"123","456","789"};
//
//        int[] phone={R.drawable.p1,R.drawable.p1,R.drawable.p1};
//
//        String[] price={"iphone14","iphone15","iphone16"};
//
//        String[] config={"5999","8999","9999"};

        int[] avtar={R.drawable.avtar1,R.drawable.avtar2,R.drawable.avtar3,R.drawable.avtar4,R.drawable.avtar5,R.drawable.avtar6,R.drawable.avtar7,R.drawable.avtar8};

        String[] username={"小张","小王","小李","小红","小军","小东","小周","小徐"};

        String[] sex={"男","女","男","男","男","女","女","女"};
        for(int i=0;i<username.length;i++){
            Map<String,Object> map=new HashMap<>();
            map.put("sex",sex[i]);
            map.put("avatar",avtar[i]);
            map.put("username",username[i]);
            map.put("arrow",R.drawable.arrow);
            list.add(map);
        }
//        for(int i=0;i<name.length;i++){
//            Map<String,Object> map=new HashMap<>();
//            map.put("name",phone[i]);
//            map.put("price",price[i]);
//            map.put("config",config[i]);
//            list.add(map);
//        }

        context=getContext();

        adapter=new Adapter(getContext(),list);

        LinearLayoutManager manager=new LinearLayoutManager(getContext());

        manager.setOrientation(RecyclerView.VERTICAL);

        recyclerView.setAdapter(adapter);

        recyclerView.setLayoutManager(manager);

        Intent intent=new Intent(context, MyService.class);

        ServiceConnection connection=new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                mybinder=(MyService.Mybinder) iBinder;
                mybinder.todo();
            }

            @Override
            public void onServiceDisconnected(ComponentName componentName) {
                mybinder=null;
            }
        };

        context.bindService(intent,connection,Context.BIND_AUTO_CREATE);

        // Inflate the layout for this fragment
        return view;
    }
}